'
'   XC Engine (project Minotaur)
'   Copyright (C) 2009 Stanislav Hordiyenko
'
'   This program is free software; you can redistribute it and/or modify
'   it under the terms of the GNU General Public License as published by
'   the Free Software Foundation; either version 3 of the License, or
'   (at your option) any later version.
'
'   This program is distributed in the hope that it will be useful,
'   but WITHOUT ANY WARRANTY; without even the implied warranty of
'   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'   GNU General Public License for more details.
'
'   You should have received a copy of the GNU General Public License
'   along with this program; if not, see http://www.gnu.org/licenses
'   or write to the Free Software Foundation,Inc., 51 Franklin Street,
'   Fifth Floor, Boston, MA 02110-1301 USA
'

Class ReportManager
	Private objIeReport
	Private strReportPath
	Private objFso

	' == Property: CreateDetailedReport =========================================
	Private blnCreateDetailedReport
	
	Public Property Get CreateDetailedReport
		CreateDetailedReport = blnCreateDetailedReport
	End Property
	
	Public Property Let CreateDetailedReport(ByVal varValue)
		blnCreateDetailedReport = varValue
	End Property
	
	' == Property: TestSuite =========================================
	Private objTestSuite
	
	Public Property Let TestSuite(ByVal varValue)
		Set objTestSuite = varValue
	End Property
	
	' == Property: UniqueReportName =========================================
	Private strUniqueReportName
	
	Public Property Get UniqueReportName
		UniqueReportName = strUniqueReportName
	End Property
	
	Public Property Let UniqueReportName(ByVal varValue)
		strUniqueReportName = varValue
	End Property
	
	Private Sub Class_Initialize()
		Set objFso = WScript.CreateObject("Scripting.FileSystemObject")
		
		blnCreateDetailedReport = True
	End Sub
	
	Private Sub Class_Terminate()
		Set objIeReport = Nothing
		Set objFso = Nothing
	End Sub
	
	Public Sub Initialize()
		strReportPath = GLOBAL_APPLICATION_PATH & "reports\" & GLOBAL_TESTSUITE & " " & GLOBAL_SESSION_UNIQUE_NUMBER
	
		objFso.CreateFolder(strReportPath)
	
		Call GenerateGeneralReport()
	End Sub
	
	Public Sub GenerateGeneralReport()
		Set objTemplateEngine = New TemplateEngine
		Set objGeneralReportFile = objFso.CreateTextFile(strReportPath & "\General Report.html", True)
		
		objTemplateEngine.LoadTemplate GLOBAL_APPLICATION_PATH & "config\templates\Report. General Information.tpl"
		objTemplateEngine.PutVariable "ReportTitle", "General Report: " & GLOBAL_TESTSUITE
		objTemplateEngine.PutVariable "EnvironmentName", GLOBAL_ENVIRONMENT
		objTemplateEngine.PutVariable "UserName", GLOBAL_USERNAME
		objTemplateEngine.PutVariable "TestSuiteName", GLOBAL_TESTSUITE
		objTemplateEngine.PutVariable "TestSuite", objTestSuite
		objTemplateEngine.PutVariable "CreateDetailedReport", blnCreateDetailedReport
		objTemplateEngine.Render()
		
		On Error Resume Next
			objGeneralReportFile.Write(objTemplateEngine.Output)
		On Error GoTo 0
		
		objGeneralReportFile.Close()
	End Sub
	
	Public Sub GenerateDetailedReport(ByVal intTestScriptIndex, ByVal intIterationIndex)
		Set objTestScript = objTestSuite(intTestScriptIndex)
	
		If objTestScript.Iterate Then
			strDetailedReportFileName = objTestScript.GetTestScriptInfo("Name") & " (iteration " & intIterationIndex & ").html"
		Else
			strDetailedReportFileName = objTestScript.GetTestScriptInfo("Name") & ".html"
		End If
	
		Set objTemplateEngine = New TemplateEngine
		Set objDetailedReportFile = objFso.CreateTextFile(strReportPath & "\" & strDetailedReportFileName, True)
		
		objTemplateEngine.LoadTemplate GLOBAL_APPLICATION_PATH & "config\templates\Report. Detailed Test Case.tpl"
		objTemplateEngine.PutVariable "DetailedReportTitle", ""
		objTemplateEngine.PutVariable "TestScript", objTestScript
		objTemplateEngine.PutVariable "TestResult", objTestResult
		objTemplateEngine.PutVariable "TestScriptIteration", objTestScript.Item(intIterationIndex)
		objTemplateEngine.Render()
		
		On Error Resume Next
			objDetailedReportFile.Write(objTemplateEngine.Output)
		On Error GoTo 0
		
		objDetailedReportFile.Close()
	End Sub
	
	'Public Sub GenerateQuickXmlReport()
	'End Sub
	
	Public Sub SendTestSummaryReport()
		'strPathToReport = "file://" & Replace(strReportPath & "\General Report.html", "\", "\\")
	
		'Set objMailMessage = WScript.CreateObject("CDO.Message")

		'objMailMessage.From = ""
		'objMailMessage.To = ""
		'objMailMessage.Subject = "Test Summary Report. Nightly Build " & GLOBAL_SESSION_UNIQUE_NUMBER
		'objMailMessage.CreateMHTMLBody strReportPath & "\General Report.html"
		'objMailMessage.CreateMHTMLBody strPathToReport

		'With objMailMessage.Configuration.Fields
		'	.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
		'	.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = ""
		'	.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
		'	.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = ""
		'	.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = ""
		'	.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
		'	.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False
		'	.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
		'	
		'	.Update
		'End With
		
		'objMailMessage.Send()
	End Sub
End Class